﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace SC.CodeBuilder
{
    partial class Builder
    {
        #region 生成DALFactory
        /// <summary>
        /// 生成DALFactory
        /// </summary>
        /// <returns></returns>
        public StringBuilder BuildeDALFactorySB()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Reflection;");
            sb.AppendLine("using System.Configuration;");
            sb.AppendLine("using System.Web;");
            sb.AppendLine();
            sb.AppendLine("using IDAL;");
            sb.AppendLine("public sealed class Factory");
            sb.AppendLine("{");
            sb.AppendLine("    private static readonly string path = ConfigurationManager.AppSettings[\"DAL\"];");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine("    /// 创建工厂  //动态new 一个实例");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine("    /// <param name=\"FactoryName\">实例名</param>");
            sb.AppendLine("    /// <returns></returns>");
            sb.AppendLine("    public static object Create(string FactoryName)");
            sb.AppendLine("    {");
            sb.AppendLine("        try { return CreateObject(path, path + \".\" + FactoryName); }");
            sb.AppendLine("        catch { return null; }");
            sb.AppendLine("    }");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine("    /// 使用缓存");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine("    /// <param name=\"path\"></param>");
            sb.AppendLine("    /// <param name=\"CacheKey\"></param>");
            sb.AppendLine("    /// <returns></returns>");
            sb.AppendLine("    private static object CreateObject(string path, string CacheKey)");
            sb.AppendLine("    {");
            sb.AppendLine("        object objType = DataCache.GetCache(CacheKey);");
            sb.AppendLine("        if (objType == null)");
            sb.AppendLine("        {");
            sb.AppendLine("            try { objType = Assembly.Load(path).CreateInstance(CacheKey); }");
            sb.AppendLine("            catch { }");
            sb.AppendLine("        }");
            sb.AppendLine("        return objType;");
            sb.AppendLine("    }");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine("    /// 不使用缓存");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine("    /// <param name=\"path\"></param>");
            sb.AppendLine("    /// <param name=\"CacheKey\"></param>");
            sb.AppendLine("    /// <returns></returns>");
            sb.AppendLine("    private static object CreateObjectNoCache(string path, string CacheKey)");
            sb.AppendLine("    {");
            sb.AppendLine("        try { return Assembly.Load(path).CreateInstance(CacheKey); }");
            sb.AppendLine("        catch { return null; }");
            sb.AppendLine("    }");
            sb.AppendLine("}");
            sb.AppendLine("/// <summary>");
            sb.AppendLine("/// 缓存相关操作类");
            sb.AppendLine("/// </summary>");
            sb.AppendLine("public class DataCache");
            sb.AppendLine("{");
            sb.AppendLine("    public DataCache() { }");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine("    /// 获取当前应用程序指定的CacheKey的Cache值");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine("    /// <param name=\"CacheKey\"></param>");
            sb.AppendLine("    /// <returns></returns>");
            sb.AppendLine("    public static object GetCache(string CacheKey)");
            sb.AppendLine("    {");
            sb.AppendLine("        System.Web.Caching.Cache objCache = HttpRuntime.Cache;");
            sb.AppendLine("        return objCache[CacheKey];");
            sb.AppendLine("    }");
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine("    /// <summary>");
            sb.AppendLine("    /// 将指定类缓存");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine("    /// <param name=\"CacheKey\"></param>");
            sb.AppendLine("    public static void SetCache(string CacheKey, object objObject)");
            sb.AppendLine("    {");
            sb.AppendLine("        System.Web.Caching.Cache objCache = HttpRuntime.Cache;");
            sb.AppendLine("        objCache.Insert(CacheKey, objObject);");
            sb.AppendLine("    }");
            sb.AppendLine("}");
            sb.AppendLine();
            return sb;
        }
        #endregion
    }
}
